﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;
using UI;
using System;


/// <summary>
/// 连充送礼
/// </summary>
public class UIActivityContinueChargeGift : UIActivityTemplate
{
    public enum GiftState
    {
        Recharge,
        Receive,
        End
    }
    public enum PickOne
    {
        Sure,
        No,
    }

    public GameObject buttomIconPref;
    public GameObject topIconPref;
    public GameObject commIconPref;

    public GameObject btnCharge;
    public GameObject imgGetted;
    public UISprite wordOnBtn;

    public GameObject bigGiftBtn;
    public GameObject bigGiftGet;

    public UIGrid buttomSimpleGrid;
    public UIGrid topSimpleGrid;
    public UIScrollView topScrollView;
    public UIScrollView buttomScrollView;
    public GameObject bigGrid;

    public UILabel Day;
    public UILabel value;

    public UILabel toDayCharedDiamond;
    public UILabel time;

    public GameObject bigGiftEffect;

    //public delegate void SetDownArrow(bool b);
    //public SetDownArrow setDownArrow;
    PickOne pickOneOrGetAll
    {
        get
        {
            if (curActivityItem != null)
            {
                return (PickOne)curActivityItem.pickOneOrGetAll;
            }
            return PickOne.No;
        }
    }
    int curIndex
    {
        get
        {
            if (curActivityItem != null)
            {
                return curActivityItem.index;
            }
            return -1;
        }
    }
    GiftState curStatues
    {
        get
        {
            if (curActivityItem != null)
            {
                return (GiftState)curActivityItem.status;
            }
            return GiftState.Recharge;
        }
    }
    /// <summary>
    /// 最终大奖状态，0：不可领取  1：可领取 2：已领取
    /// </summary>
    int bigGiftStatues = 0;
    public void SetBigGiftStatus(int status)
    {
        bigGiftStatues = status;
    }
    public int GetBigGiftStatus()
    {
        return bigGiftStatues;
    }


    DateTime startTime;
    DateTime endTime;
    //DateTime rewardEndTime;


    public List<ActivityItem> itemList = new List<ActivityItem>();
    ActivityItem curActivityItem = null;
    List<BaseResource> bigGift = new List<BaseResource>();


    public override void Show()
    {
        base.Show();
        CSContinuedRechargeInfo msg = new CSContinuedRechargeInfo();
        msg.activityId = this.activityId;
        NetAPI.SendHttp(OpDefine.CSContinuedRechargeInfo, msg, MsgCallback);
    }

    public override void OnCoverPageRemove()

    {
        base.OnCoverPageRemove();
        if (activity.panel.activedType == activityType)
        {
            CSContinuedRechargeInfo msg = new CSContinuedRechargeInfo();
            msg.activityId = this.activityId;
            NetAPI.SendHttp(OpDefine.CSContinuedRechargeInfo, msg, MsgCallback);
        }

    }
    public override void OnCoverPageOpen()
    {
        base.OnCoverPageOpen();
        bigGiftEffect.SetActive(false);

        //if (setDownArrow != null)
        //{
        //    setDownArrow(false);
        //}
    }



    void MsgCallback(NetWork.Packets.Packet packet, bool success)
    {
        if (success)
        {
            SCContinuedRechargeInfo msg = packet.kBody as SCContinuedRechargeInfo;

            if (msg.itemList.Count <= 0)
                return;

            bigGift.Clear();
            itemList.Clear();

            activityId = msg.activityId;
            itemList = msg.itemList;
            curActivityItem = GetActivityItem(msg.currentIndex);
            bigGift = msg.bigReward;
            bigGiftEffect.SetActive(true);

            toDayCharedDiamond.text= string.Format(TableTextBlo.GetText("activity_continue_recharge_gift_top_5").Replace("\\r", "\r").Replace("\\n", "\n"), curActivityItem.param);
            SetBigGiftStatus(msg.status);

            startTime = ServerTimerTool.Java2CSTimeLocale(msg.startTime);
            endTime = ServerTimerTool.Java2CSTimeLocale(msg.endTime);
            //rewardEndTime = ServerTimerTool.Java2CSTimeLocale(msg.rewardEndTime);
            time.text = String.Format(TableTextBlo.GetText("activity_continue_recharge_gift_time").Replace("\\r", "\r").Replace("\\n", "\n"), startTime.Month, startTime.Day, startTime.Hour,startTime.Minute,startTime.Second, endTime.Month, endTime.Day, endTime.Hour, endTime.Minute, endTime.Second);


            Day.text = msg.itemList.Count.ToString();

            if (curActivityItem != null)
            {
                UpdateRewards();
            }
            if (itemList.Count > 0)
            {
                UpdateRewardsButtom();
            }
            if (bigGift.Count > 0)
            {
                UpdateRewardsBigGift();
            }
        }
    }


    public void OnRechargeBtn()
    {
        if (curStatues == GiftState.Recharge)
        {
            PageManager.Instance.OpenPage("ChargePage", "");
        }
        else
        {
            if (pickOneOrGetAll == PickOne.No)
            {
                GetAllReward();
            }
            else if (pickOneOrGetAll == (int)PickOne.Sure)
            {
                ShowSelectGiftPanel(curActivityItem);
            }
            else
                Debug.LogError("Sever PickOneOrGetAll State Error!");
        }
    }

    ActivityItem GetActivityItem(int index)
    {
        foreach (var item in itemList)
        {
            if (item.index == index)
            {
                return item;
            }
        }
        return null;
    }

    public void GetAllReward()
    {
        if (curActivityItem != null)
        {
            GetReward(RewardType.Normal, curActivityItem.index, curActivityItem.rewardList, (pack, b) =>
            {
                this.GetRewardCallback(pack, b);
                if (b)
                {
                    OnCoverPageRemove();
                }
            });
        }
    }


    public void OnGetBigGiftBtn()
    {
        GetReward(RewardType.BigGift, -1, bigGift, (pack, b) =>
        {
            this.GetRewardCallback(pack, b);
            if (b)
            {
                OnCoverPageRemove();
            }
        });
    }

    public enum RewardType
    {
        Normal = 0,
        BigGift = 1,
    }

    public void GetReward(RewardType type, int index, List<BaseResource> res, NetWork.Layer.HttpHandler handler)
    {
        CSContinuedRechargeReward msg = new CSContinuedRechargeReward();
        msg.activityId = this.activityId;
        msg.index = index;
        msg.type = (int)type;
        msg.getResources.AddRange(res);
        NetAPI.SendHttp(OpDefine.CSContinuedRechargeReward, msg, handler);
    }


    #region GetGiftPanel
    public ContinueGiftGet getGiftPanel;
    public void ShowGetGiftPanel(ActivityItem item)
    {
        getGiftPanel.SetData(item);
        getGiftPanel.Show();
    }

    public void HideGetGiftPanel()
    {
        getGiftPanel.Hide();
        OnCoverPageRemove();
    }
    #endregion

    #region SelectGiftPanel
    public SelectRewardBySever selectGiftPanel;
    public void ShowSelectGiftPanel(ActivityItem item)
    {
        selectGiftPanel.SetData(item);
        selectGiftPanel.Show();
    }
    public void HideSelectGiftPanel()
    {
        selectGiftPanel.Hide();
        OnCoverPageRemove();
    }
    #endregion

    #region
    public ShowCCGift showCCGift;

    public void ShowShowCCGiftPanel(ActivityItem item)
    {
        showCCGift.SetData(item);
        showCCGift.Show();
    }

    public void HidShowCCGiftPanel()
    {
        showCCGift.Hide();
    }
    #endregion

    void GetRewardCallback(NetWork.Packets.Packet packet, bool success)
    {
        if (success)
        {
            SCContinuedRechargeReward msg = packet.kBody as SCContinuedRechargeReward;
            StaticData.playerData.AddResultObj(msg.get);
            SetBigGiftStatus(msg.status);
            CommonMethod.ShowGetObjs(msg.get);
            bool flag = true;
            foreach (var item in msg.itemList)
            {
                if (item.status == 1)
                {
                    flag = false;
                    break;
                }
            }
            if (flag && bigGiftStatues!=1)
            {
                StaticData.redTips.SetPointState(RedPointTip.Red.continuedRecharge, 0);
                StaticData.redTips.ReduceActivitys();
            }
        }
    }

    void UpdateRewards()
    {
        topSimpleGrid.transform.DestroyChildren();

        value.text = curActivityItem.value;
        UpdateRewardsTop(curActivityItem.rewardList);

        switch (curStatues)
        {
            case GiftState.Recharge:
                wordOnBtn.spriteName = "UI_activity_shouchong_chognzhi";
                btnCharge.SetActive(true);
                imgGetted.SetActive(false);
                break;
            case GiftState.Receive:
                wordOnBtn.spriteName = "UI_activity_shouchong_lignqu";
                btnCharge.SetActive(true);
                imgGetted.SetActive(false);
                break;
            case GiftState.End:
                btnCharge.SetActive(false);
                imgGetted.SetActive(true);
                break;
            default:
                Debug.LogError("Current Statue Error!");
                break;
        }

    }

    void UpdateRewardsTop(List<BaseResource> itemList)
    {
        foreach (var item in itemList)
        {
            GameObject obj = Instantiate(topIconPref);
            obj.SetActive(true);
            obj.GetComponent<CommonIcon>().SetData(item.id, true, item.num);

            topSimpleGrid.AddChild(obj.transform);
            obj.transform.localScale = Vector3.one;
            obj.transform.localPosition = Vector3.zero;
            obj.transform.localEulerAngles = Vector3.zero;
        }
        topSimpleGrid.Reposition();
        topScrollView.ResetPosition();
        topIconPref.SetActive(false);
    }
    void UpdateRewardsBigGift()
    {
        bigGrid.transform.DestroyChildren();
        GameObject obj = Instantiate(commIconPref);
        obj.SetActive(true);
        var script = obj.GetComponent<CommonIcon>();
        script.SetData(bigGift[0].id, true, -1);
        obj.transform.SetParent(bigGrid.transform);
        obj.transform.localScale = Vector3.one;
        obj.transform.localPosition = Vector3.zero;
        commIconPref.SetActive(false);

        switch (bigGiftStatues)
        {
            case (int)GiftState.Recharge:
                bigGiftBtn.SetActive(false);
                bigGiftGet.SetActive(true);
                bigGiftGet.GetComponent<UISprite>().spriteName = "UI_huodong_word_zuizhongdajiang";
                bigGiftGet.GetComponent<UISprite>().MakePixelPerfect();
                break;
            case (int)GiftState.Receive:
                bigGiftBtn.SetActive(true);
                bigGiftGet.SetActive(false);
                break;
            case (int)GiftState.End:
                bigGiftBtn.SetActive(false);
                bigGiftGet.SetActive(true);
                bigGiftGet.GetComponent<UISprite>().spriteName = "UI_activity_meiriqiandao_yilingqu";
                bigGiftGet.GetComponent<UISprite>().MakePixelPerfect();
                break;
            default:
                Debug.LogError("Big Gift Error!");
                break;
        }
    }
    void UpdateRewardsButtom()
    {
        buttomSimpleGrid.transform.DestroyChildren();
        buttomIconPref.SetActive(true);
        foreach (var item in itemList)
        {
            GameObject obj = Instantiate(buttomIconPref);
            obj.SetActive(true);
            var script = obj.GetComponent<ContinueRechargeIcon>();
            script.SetButtomData(item, item.index == curIndex);
            buttomSimpleGrid.AddChild(obj.transform);
            obj.transform.localEulerAngles = Vector3.zero;
            obj.transform.localPosition = Vector3.zero;
            obj.transform.localScale = Vector3.one;
        }
        buttomSimpleGrid.Reposition();
        buttomScrollView.ResetPosition();
        buttomIconPref.SetActive(false);
    }


    protected override void PrepareData()
    {
        this.activityType = ActivityType.continueRechargeGift;
    }
}
